package com.uchedao.vehicledetection.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.ScrollView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.uchedao.mybaselibrary.http.VolleyUtil;
import com.uchedao.mybaselibrary.utils.LogUtil;
import com.uchedao.vehicledetection.R;
import com.uchedao.vehicledetection.adapter.MyCarSourceAdapter;
import com.uchedao.vehicledetection.config.Api;
import com.uchedao.vehicledetection.loadstate.UCheDaoLocalStateRelativityLayout;
import com.uchedao.vehicledetection.model.MyCarResouceEntity;
import com.uchedao.vehicledetection.model.response.MyCarListResponse;
import com.uchedao.vehicledetection.network.HttpRequest;
import com.uchedao.vehicledetection.view.widget.pulltorefresh.library.PullToRefreshBase;
import com.uchedao.vehicledetection.view.widget.pulltorefresh.library.PullToRefreshListView;
import com.uchedao.vehicledetection.view.widget.pulltorefresh.library.PullToRefreshScrollView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: wushan
 * Date: 2015-09-10
 * Time: 18:09
 */
public class NormalCheckCarView extends UCheDaoLocalStateRelativityLayout {

    private PullToRefreshListView lvData;
    private PullToRefreshScrollView svNoData;

    private Context activity;
    private MyCarSourceAdapter adapter;
    private boolean isLoaded;
    private int pageNo;
    private int pageSize;

    public NormalCheckCarView(Context context) {
        super(context);
    }

    public NormalCheckCarView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NormalCheckCarView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public View getContentView() {
        return null;
    }

    @Override
    public void invalidateViews() {

    }

    @Override
    public void initView() {
        LayoutInflater.from(mContext).inflate(R.layout.view_normal_check_car, this);
        svNoData = (PullToRefreshScrollView) findViewById(R.id.svNodate);
        svNoData.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        lvData = (PullToRefreshListView) findViewById(R.id.lvNormalCheckCar);
        lvData.setMode(PullToRefreshBase.Mode.BOTH);
    }

    @Override
    public void initDataAfterView() {
    }

    @Override
    public void initListener() {
        svNoData.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
                pageNo = 0;
                loadData(pageNo, pageSize);
            }
        });

        lvData.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageNo = 0;
                loadData(pageNo, pageSize);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                loadData(pageNo, pageSize);
            }
        });
    }

    @Override
    public void loadData(int page) {

    }

        @Override
        public boolean save() {
            return false;
        }

        @Override
        public boolean dataChanged() {
            return false;
        }

    public void init(Context activity) {
        this.activity = activity;
        if(isLoaded)
            return;

        pageNo = 0;
        pageSize = 15;
        loadData(pageNo, pageSize);
    }

    public void loadData(int pageNo, int pageSize) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("offset", pageNo*pageSize+"");
        params.put("limit", pageSize+"");
        Request req = HttpRequest.getRequest(HttpRequest.GET, Api.Action.WAIT_CAR, params,
                new Response.Listener<JsonObject>() {
                    @Override
                    public void onResponse(JsonObject response) {
                        svNoData.onRefreshComplete();
                        lvData.onRefreshComplete();
                        MyCarListResponse entity = new Gson().fromJson(response, MyCarListResponse.class);
                        updateView(entity.getList());
                        isLoaded = true;
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        svNoData.onRefreshComplete();
                        lvData.onRefreshComplete();
                        LogUtil.e(error.getMessage());
                    }
                });
        VolleyUtil.getInstance(activity).getRequestQueue(activity).add(req);
    }

    public void updateView(List<MyCarResouceEntity> list) {
        if(list!=null && list.size()>0)
            svNoData.setVisibility(View.GONE);

        if(pageNo==0 || adapter==null) {
            adapter = new MyCarSourceAdapter(mContext, list);
            adapter.style = 1;
            lvData.setAdapter(adapter);
        }
        if(pageNo > 0) {
            adapter.getmData().addAll(list);
            adapter.notifyDataSetChanged();
        }
        pageNo++;
    }
}
